/**
 * 
 */
package vn.lumi.smarthome.network;

import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_Room;
import vn.lumi.smarthome.service.VibrateThread;
import vn.lumi.smarthome.ulti.Preference;
import android.content.Context;
import android.database.Cursor;

/**
 * @author HUYHIEU
 * 
 */

public class CautionPrMessage extends HCMsg {

	private final static int IndexRomId = 1;
	private final static int IndexState = 2;

	private boolean isCorrectFormat = false;
	private int romID = 0;
	private int state = 0;

	public CautionPrMessage() {
		super(NetMessage.CMD_HC_CAUTION_PR, 0);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.HCMsg#parseMessage(byte[], int)
	 */
	@Override
	public void parseMessage(byte[] input, int length) {
		// TODO Auto-generated method stub
		isCorrectFormat = isCorrectFormat(input, length);
		if (isCorrectFormat == false)
			return;
		romID = Byte2Unsigned(input[IndexRomId]);
		state = Byte2Unsigned(input[IndexState]);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * vn.lumi.smarthome.network.HCMsg#doWhenReceived(android.content.Context)
	 */
	@Override
	public void doWhenReceived(Context context) {
		// TODO Auto-generated method stub
		if (romID == 0) {
			if (state == 0x02) {
				// Toast.makeText(context, "Vừa có người xuất hiện",
				// Toast.LENGTH_LONG).show();
				vn.lumi.smarthome.Config.showToast(context, context
						.getString(R.string.txt_msg_alarm_pir_not_rom_type));
				if (Preference.getVibratePr(context))
					new VibrateThread(context, 1000).start();
			}
		} else {
			if (state == 0x02) {
				String[] projection = { TBL_Room.NAME };
				String selection = TBL_Room._ID + "=?";
				String[] selectionArgs = { romID + "" };
				Cursor cursor = context.getContentResolver().query(
						TBL_Room.CONTENT_URI_ROOM, projection, selection,
						selectionArgs, null);
				cursor.moveToFirst();
				// Toast.makeText(
				// context,
				// "Vừa có người xuất hiện trong phòng "
				// + cursor.getString(cursor
				// .getColumnIndex(TBL_Room.NAME)),
				// Toast.LENGTH_LONG).show();
				vn.lumi.smarthome.Config.showToast(
						context,
						context.getString(R.string.txt_msg_alarm_pir_have_rom_type)
								+ " "
								+ cursor.getString(cursor
										.getColumnIndex(TBL_Room.NAME)));
				cursor.close();
				cursor = null;
				if (Preference.getVibratePr(context))
					new VibrateThread(context, 1000).start();
			}
		}

	}
}
